﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Framework.Event;
using Framework.Bus;
using Framework.Infrastructure;
using Microsoft.Practices.Unity;
using System.Reflection;

namespace Framework.Domain
{
    public abstract class AggregateRoot : Entity, IAggregateRoot
    {
        internal static IMessageDispatcher EventDispatcher = new MessageDispatcher();
        // per request life time 
        IEventBus EventBus { 
            get {
                return IoCFactory.Resolve<IEventBus>();
            }
        }

        protected void OnEvent(IDomainEvent @event)
        {
            HandleEvent(@event);
            EventBus.Publish(@event);
        }

        private void HandleEvent(IDomainEvent @event)
        {
            EventDispatcher.DispatchMessage(@event, this);
        }
    }
}
